
package com.xmcowell.sportpacemaster.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;

import com.xmcowell.sportpacemaster.layout.entity.DaoMaster;
import com.xmcowell.sportpacemaster.layout.entity.UserInfo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/10/19.
 */

public class DensityUtil {
    private static final String TAG = "DensityUtil";


    /**
     * 根据手机的分辨率，从dp转px
     *
     * @param context
     * @param dpValue
     * @return
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率，从px转dp
     *
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     *
     * @param pxValue
     * @param pxValue
     *            （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param spValue
     * @param spValue
     *            （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static boolean isMobileNO(String mobiles) {
        Pattern p = Pattern
                .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 获取手机屏幕的宽度
     *
     * @param context
     * @return
     */
    public static int getSystemWidth(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int w_screen = dm.widthPixels;
        return w_screen;
    }

    /**
     * 获取手机屏幕的高度
     *
     * @param context
     * @return
     */
    public static int getSystemHeight(Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int h_screen = dm.heightPixels;
        return h_screen;
    }

    /**
     * 获取view的宽高
     *
     * @param flag 为1时，获取宽，否则获取高
     * @param view
     * @return
     */
    public static int getViewWidthOrHeight(int flag, View view) {
        int size;
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        if (flag == 1) {
            size = view.getMeasuredWidth();
        } else {
            size = view.getMeasuredHeight();
        }
        return size;
    }

    /**
     * File转String
     */
    public static String file2String(File file) throws IOException {
        if (file == null) {
            return "";
        }
        BufferedReader bf = new BufferedReader(new FileReader(file));
        String content = "";
        StringBuilder sb = new StringBuilder();
        while (content != null) {
            content = bf.readLine();
            if (content == null) {
                break;
            }
            sb.append(content.trim());
        }
        bf.close();
        return sb.toString();
    }

    public static String getUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

    /**
     * 定义一个方法用来格式化音乐的时间
     */
    public static String formatTime(long time) {
        long min = time / 1000 / 60;
        long second = time / 1000 % 60;
        String m = "";
        String s = "";
        if (min == 0) {
            m = "00";
        } else if (min < 10) {
            m = "0" + min;
        } else {
            m = "" + min;
        }
        if (second == 0) {
            s = "00";
        } else if (second < 10) {
            s = "0" + second;
        } else {
            s = "" + second;
        }
        return m + ":" + s;
    }

    /**
     * 获取Met
     * @param cal 卡路里
     * @param time 运动时间 单位秒
     * @return
     */
    public static double getMet(double cal,int time){
        UserInfo userInfo = DaoManager.getInstance().getUserInfo();
        if (userInfo == null){
            return 0;
        }
        if (userInfo.getWeight() == 0){
            return 0;
        }
        if (time == 0){
            return 0;
        }
        double met = cal/(time/60)/(userInfo.getWeight()*1f)/0.0167;
        return met;
    }

}

